<?php

require_once("evalable.php");
require_once("logicobject.php");

class LogicObject extends Evalable {

    var $v = null;

    function LogicObject($value) {
        if (is_object($value) && get_class($value) == "NumberObject") {
            $this->v = $value->v;
        } else {
            $this->v = $value;
        }
    }

    function repr() {
        if ($this->v == 1) {
            return '*true*';
        } elseif ($this->v == 0) {
            return '*false*';
        } else {
            return "(logic " . $this->v . ")";
        }
    }

    function lispeval($env, $args=null) {
        return $this;
    }

    function nullp() {
        return $this->neg();
    }

    function neg() {
        return new LogicObject(1 - $this->v);
    }

    function cmp($other) {
        if (get_class($other) == "LogicObject") {
            if ($this->v == $other->v) {
                return 0;
            } elseif ($this->v > $other->v) {
                return 1;
            } else {
                return -1;
            }
        } else {
            if ($this->v == $other) {
                return 0;
            } elseif ($this->v > $other) {
                return 1;
            } else {
                return -1;
            }
        }
    }

    function logic_and($other) {
        if ($this->v > $other->v) {
            return new LogicObject($other->v);
        } else {
            return new LogicObject($this->v);
        }
    }

    function logic_or($other) {
        if ($this->v < $other->v) {
            return new LogicObject($other->v);
        } else {
            return new LogicObject($this->v);
        }
    }

}